package com.ruoyi.blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.blog.domain.TbBlogDto;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.blog.mapper.TbBlogMapper;
import com.ruoyi.blog.domain.TbBlog;
import com.ruoyi.blog.service.ITbBlogService;

/**
 * 探店博客Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-10
 */
@Service
public class TbBlogServiceImpl implements ITbBlogService 
{
    @Autowired
    private TbBlogMapper tbBlogMapper;

    /**
     * 查询探店博客
     * 
     * @param id 探店博客主键
     * @return 探店博客
     */
    @Override
    public TbBlog selectTbBlogById(Long id)
    {
        return tbBlogMapper.selectTbBlogById(id);
    }

    /**
     * 查询探店博客列表
     * 
     * @param tbBlog 探店博客
     * @return 探店博客
     */
    @Override
    public List<TbBlogDto> selectTbBlogList(TbBlog tbBlog)
    {
        List<TbBlogDto> newList = new ArrayList<>();
        List<TbBlog> list = tbBlogMapper.selectTbBlogList(tbBlog);
        for(TbBlog tbBlogOne : list){
            TbBlogDto tbBlogDto = new TbBlogDto();
            Long shopId = tbBlogOne.getShopId();
            Long userId = tbBlogOne.getUserId();
            String shopName = tbBlogMapper.selectTbShopNameById(shopId);
            String userName = tbBlogMapper.selectTbUserNameById(userId);
            tbBlogDto.setShopName(shopName);
            tbBlogDto.setUserName(userName);
            tbBlogDto.setId(tbBlogOne.getId());
            tbBlogDto.setShopId(tbBlogOne.getShopId());
            tbBlogDto.setUserId(tbBlogOne.getUserId());
            tbBlogDto.setTitle(tbBlogOne.getTitle());
            tbBlogDto.setContent(tbBlogOne.getContent());
            tbBlogDto.setImages(tbBlogOne.getImages());
            tbBlogDto.setLiked(tbBlogOne.getLiked());
            tbBlogDto.setComments(tbBlogOne.getComments());
            tbBlogDto.setStatus(tbBlogOne.getStatus());
            tbBlogDto.setIsDeleted(tbBlogOne.getIsDeleted());
            newList.add(tbBlogDto);
        }
        return newList;
    }

    /**
     * 新增探店博客
     * 
     * @param tbBlog 探店博客
     * @return 结果
     */
    @Override
    public int insertTbBlog(TbBlog tbBlog)
    {
        tbBlog.setCreateTime(DateUtils.getNowDate());
        return tbBlogMapper.insertTbBlog(tbBlog);
    }

    /**
     * 修改探店博客
     * 
     * @param tbBlog 探店博客
     * @return 结果
     */
    @Override
    public int updateTbBlog(TbBlog tbBlog)
    {
        tbBlog.setUpdateTime(DateUtils.getNowDate());
        return tbBlogMapper.updateTbBlog(tbBlog);
    }

    /**
     * 批量删除探店博客
     * 
     * @param ids 需要删除的探店博客主键
     * @return 结果
     */
    @Override
    public int deleteTbBlogByIds(Long[] ids)
    {
        return tbBlogMapper.deleteTbBlogByIds(ids);
    }

    /**
     * 删除探店博客信息
     * 
     * @param id 探店博客主键
     * @return 结果
     */
    @Override
    public int deleteTbBlogById(Long id)
    {
        return tbBlogMapper.deleteTbBlogById(id);
    }

    @Override
    public boolean updateBlogStatus(Long id, Integer status) {
        // 验证状态值是否合法
        if (status != 0 && status != 1) {
            throw new IllegalArgumentException("状态值不合法");
        }
        return tbBlogMapper.updateBlogStatus(id, status) > 0;
    }
}
